package com.rd.student.ruanjian.class3.gaochenxi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class People {
   public String name;
   private int age;

   public People() {
   }

   public People getPeople() {
      return new People();
   }

   public void eat() {
      System.out.println("eat something");
   }

   private void say() {
      System.out.println("say something");
   }

   public String getName() {
      return name;
   }
}

class test {
   public static void main(String[] args) {
      People people = new People();
      Class cc = people.getClass();

      Class cc1 = People.class;
      Class cc2;

      {
         try {
            cc2 = Class.forName("com.rd.student.ruanjian.class3.gaochenxi.People");
            System.out.println(cc2.getName());
            Field[] fields = cc1.getFields();//将对应类的属性放到fields数组当中，getFields()只获取共有的属性名
            for (Field field : fields) {
               System.out.print(field.getName() + " ");
            }
            System.out.println();
            System.out.println("==================");
            Field[] fields1 = cc1.getDeclaredFields();
            for (Field field : fields1) {
               System.out.print(field.getName() + " ");
            }
            System.out.println();
            System.out.println("==================");
            Method[] methods = cc2.getMethods();//获取所有的共有的方法,包括底层的，和People自己的
            for (Method method : methods) {
               System.out.print(method.getName() + " ");
            }
            System.out.println();
            System.out.println("==================");
            Method[] methods1 = cc2.getDeclaredMethods();//获取当前People自己定义的public和private修饰的方法
            for (Method method : methods1) {
               System.out.print(method.getName() + " ");
            }
            System.out.println();
            System.out.println("==================");
            Field f1 = cc2.getField("name");
            Object p1 = cc2.newInstance();
            f1.set(p1, "Tom");//我可以用代码给属性赋值
            System.out.println(f1.get(p1));
            System.out.println();
            System.out.println("==================");

         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchFieldException e) {
            throw new RuntimeException(e);
         }
      }
   }
}
